<?php
namespace App\Logic\Stocking;

use App\Models\Basics\SysICountry;
use App\Models\Basics\Transport;
use App\Models\Product\ProductAmazonSku;
use App\Models\Stocking\StockingSetDate;
use Exception;
class SyncSetDate
{
    /**
     * 设定：每天运行
     */
    public static function exe(){
        $pas = ProductAmazonSku::query()
            ->where('status',1)
            ->whereHas('skuClassify',function ($query){
                $query->where('product_classify.status',1);
            })
            ->get();
        $area = [];
        $new = [];
        foreach($pas as $_pas){
            if(!array_key_exists($_pas->country_id,$area)){
                $area[$_pas->country_id] = SysICountry::getAreaIdByCountryId($_pas->country_id);
            }
            $areaId = $area[$_pas->country_id];
            $classifyId = $_pas->skuClassify->id ?? 0;
            if(!$classifyId)continue;
            $value = $areaId.'_'.$classifyId;
            if(!in_array($value,$new)){
                $new[] = $value;
            }
        }
        $new = $new ? array_unique($new) : [];
        $count = count($new);
        if($count < 50){
            report(new Exception("Failed to artisan sync:set-date : count(" . $count . ")总数异常",123));
            die(0);
        }
        $ssd = StockingSetDate::all();
        $old = [];
        foreach($ssd as $_ssd){
            $value = $_ssd->area_id.'_'.$_ssd->classify_id;
            if(!in_array($value,$old)){
                $old[] = $value;
            }
        }
        $old = $old ? array_unique($old) : [];
        //需要删除的数据
        $delete = array_diff($old,$new);
        $country = [];
        foreach($delete as $_delete){
            list($areaId,$classifyId) = explode('_',$_delete);
            if(!array_key_exists($areaId,$country)){
                $country[$areaId] = SysICountry::getCountryIdsByAreaId($areaId);
            }
            $countryIds = $country[$areaId];
            $count = ProductAmazonSku::query()
                ->whereIn('country_id',$countryIds)
                ->whereHas('skuClassify',function ($query)use ($classifyId){
                    $query->where('product_classify.id',$classifyId)->where('product_classify.status',1);
                })
                ->count();
            if($count > 0)continue;
            StockingSetDate::query()->where('area_id',$areaId)->where('classify_id',$classifyId)->where('type',1)->delete();
        }
        //需要新增的数据
        $add = array_diff($new,$old);
        $country = [];
        $pCycle = [];
        $seaDayArr = [];
        foreach($add as $_add){
            list($areaId,$classifyId) = explode('_',$_add);
            $count = StockingSetDate::query()->where('area_id',$areaId)->where('classify_id',$classifyId)->count();
            if($count > 0)continue;
            if(!array_key_exists($areaId,$country)){
                $country[$areaId] = SysICountry::getCountryIdsByAreaId($areaId);
            }
            $countryIds = $country[$areaId];
            if(!array_key_exists($_add,$pCycle)){
                $pCycle[$_add] = StockingSetDate::getPCycle($countryIds,$classifyId);
            }
            if(!array_key_exists($areaId,$seaDayArr)){
                $seaDayArr[$areaId] = Transport::getShipmentDay($areaId,1);
            }
            StockingSetDate::addSetDate($areaId,$classifyId,$pCycle[$_add],$seaDayArr[$areaId]);
        }
    }
}
